<?php
define('LIMIT_PRODUCT_WIDTH',false);
// maximum width for all product image
define('MAX_PRODUCT_IMAGE_WIDTH', 800);
// the width for product thumbnail
define('THUMBNAIL_WIDTH',132);

define('THUMBNAIL_WIDTH_100', 100);

define('THUMBNAIL_MEDIUM_IMAGE_WIDTH', 372);


class Upload
{
	function uploadFile($inputName, $uploadDir,$outputName)
	{
		$image     = $_FILES[$inputName];
		$imagePath = '';
		
	
	// if a file is given
		if (trim($image['tmp_name']) != '') 
		{
			$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
			$ext=strtolower($ext);
			// generate a random new file name to avoid name conflict
			$imagePath = $outputName .".$ext";
			$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
		}
   		return $imagePath;
	}
	
	function uploadProductImage($inputName, $uploadDir,$outputName)
	{
		$image     = $_FILES[$inputName];
		$uploadDir;
		if (! is_dir($uploadDir."fullsize"))
			mkdir( $uploadDir."fullsize" , 0777 );
		if (! is_dir($uploadDir."thumbs"))
			mkdir( $uploadDir."thumbs" , 0777 );
		if (! is_dir($uploadDir."real"))
			mkdir( $uploadDir."real" , 0777 );
		if (! is_dir($uploadDir."thumbs100"))
			mkdir( $uploadDir."thumbs100" , 0777 );
		
		$imagePath = 'real/';
		$thumbnailPath = 'thumbs/';
		$thumbnail100Path = 'thumbs100/';
		$mediumpath="fullsize/";
		// if a file is given
		if (trim($image['tmp_name']) != '') 
		{
			$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
			$ext=strtolower($ext);
			// generate a random new file name to avoid name conflict
			$imagePath .=$outputName .".$ext";
			
			list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 
	
			// make sure the image width does not exceed the
			// maximum allowed width
			
			if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) 
			{
				$result    = Upload::createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
				$imagePath = $result;
			} 
			else 
			{
				$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
			}
			
			
			if ($result) 
			{
				// create thumbnail
				$thumbnailPath .= $outputName . ".$ext";
				$thumbnail100Path .= $outputName . ".$ext";
				$mediumpath .= $outputName . ".$ext";
				
				$result = Upload::createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);
				$result100 = Upload::createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnail100Path, THUMBNAIL_WIDTH_100);
				$mendium= Upload::createThumbnail($uploadDir . $imagePath, $uploadDir . $mediumpath, THUMBNAIL_MEDIUM_IMAGE_WIDTH);
				
				// create thumbnail failed, delete the image
				if (!$result) {
					unlink($uploadDir . $imagePath);
					$imagePath = $thumbnailPath = '';
				} 
				else 
				{
					$thumbnailPath = $result;
				}	
			} 
			else 
			{
				// the product cannot be upload / resized
				$imagePath = $thumbnailPath = '';
			}
			
		}
	   	return array('image' => $imagePath, 'thumbnail' => $thumbnailPath,'mendium'=>$mendium);
	}
	function createThumbnail($srcFile, $destFile, $width, $quality = 75)
	{
		$thumbnail = '';
		
		if (file_exists($srcFile)  && isset($destFile))
		{
			$size        = getimagesize($srcFile);
			$w           = number_format($width, 0, ',', '');
			$h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
			
			$thumbnail =  Upload::copyImage($srcFile, $destFile, $w, $h, $quality);
		}
		
		// return the thumbnail file name on sucess or blank on fail
		return basename($thumbnail);
	}
	
	function createThumbnailheight($srcFile, $destFile, $height, $quality = 75)
	{
		$thumbnail = '';
		
		if (file_exists($srcFile)  && isset($destFile))
		{
			$size        = getimagesize($srcFile);
			$h           = number_format( $height,0, ',', '');
			$w           = number_format(($size[0] / $size[1]) *  $height, 0, ',', '');
			
			$thumbnail =  Upload::copyImage($srcFile, $destFile, $w, $h, $quality);
		}
		
		// return the thumbnail file name on sucess or blank on fail
		return basename($thumbnail);
	}

	function copyImage($srcFile, $destFile, $w, $h, $quality = 100)
	{
		$tmpSrc     = pathinfo(strtolower($srcFile));
		$tmpDest    = pathinfo(strtolower($destFile));
		$size       = getimagesize($srcFile);
	
		if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
		{
		   $destFile  = substr_replace($destFile, $tmpDest['extension'], -3);
		   $dest      = imagecreatetruecolor($w, $h);
		   imageantialias($dest, TRUE);
		} elseif ($tmpDest['extension'] == "png") {
		   $dest = imagecreatetruecolor($w, $h);
		   imageantialias($dest, TRUE);
		} else {
		  return false;
		}
	
		switch($size[2])
		{
		   case 1:       //GIF
			   $src = imagecreatefromgif($srcFile);
			   break;
		   case 2:       //JPEG
			   $src = imagecreatefromjpeg($srcFile);
			   break;
		   case 3:       //PNG
			   $src = imagecreatefrompng($srcFile);
			   break;
		   default:
			   return false;
			   break;
		}
	
		imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

    	switch($size[2])
    	{
       		case 1:
       		case 2:
           		imagejpeg($dest,$destFile, $quality);
           		break;
       		case 3:
           		imagepng($dest,$destFile);
    	}
    	return $destFile;

	}

	function uploadImage($inputName, $uploadDir,$outputName,$limt)
  	{
		$image     = $_FILES[$inputName];
	
	// if a file is given
		if (trim($image['tmp_name']) != '') 
		{
			$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
			$ext=strtolower($ext);
			// generate a random new file name to avoid name conflict
			$imagePath .=$outputName .".$ext";
			
			list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 
			if($width<=$limt)
				return $this->uploadFile($inputName, $uploadDir,$outputName);
			else
			{
				$result  = $this->createThumbnail($image['tmp_name'], $uploadDir . $imagePath, $limt);
				$imagePath = $result;
			}
		}
		return $imagePath;
	}
}
?>